{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ch7.9 BIC\n",
    "以西瓜数据集2.0为训练集，试基于BIC准则（贝叶斯信息准则）构建一个贝叶斯网。\n",
    "\n",
    "Note:\n",
    "\n",
    "1. 假设只有一个父节点\n",
    "2. 只画了两个图比较贝叶斯网不同导致的BIC(B|D)的不同\n",
    "3. 从所有可能的网络结构空间搜索最优贝叶斯网结构是一个NP难问题，难以快速求解，所以这里没有写搜索。\n",
    "   有两种常用的策略能在有限时间内求得近似解：（1）贪心法（2）施加约束。见P160"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import BayesNet as bn\n",
    "import numpy as np\n",
    "\n",
    "dataset = pd.read_csv('data/table_4_3_watermelon_3_0_num.csv')\n",
    "dataset = dataset.drop('Idx',axis=1)\n",
    "dataset = dataset.drop('density', axis=1)\n",
    "dataset = dataset.drop('sugar_ratio', axis=1)\n",
    "test_data = dataset.ix[0,:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "104.072550945\n"
     ]
    }
   ],
   "source": [
    "bayesNet = bn.BayesNet()\n",
    "root_node = bayesNet.createNode(dataset.columns[0],dataset)\n",
    "node = [root_node]\n",
    "for cn in dataset.columns[1:]:\n",
    "    nn = bayesNet.createNode(cn,dataset, parents=[root_node])\n",
    "    node.append(nn) \n",
    "\n",
    "bayesNet.plot_net(output_file='out/temp1.gv')\n",
    "\n",
    "s1 = bayesNet.BIC_score(dataset)\n",
    "print(s1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "88.6953656447\n"
     ]
    }
   ],
   "source": [
    "bayesNet = bn.BayesNet()\n",
    "root_node = bayesNet.createNode(dataset.columns[4],dataset)\n",
    "node = [root_node]\n",
    "for cn in dataset.columns:\n",
    "    if cn != 'navel':\n",
    "        nn = bayesNet.createNode(cn,dataset, parents=[root_node])\n",
    "        node.append(nn) \n",
    "bayesNet.plot_net(output_file='out/temp2.gv')\n",
    "\n",
    "s2 = bayesNet.BIC_score(dataset)\n",
    "print(s2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda root]",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  },
  "toc": {
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": "block",
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
